//
//  EpointUpdateService.m
//  EpointFrame5
//
//  Created by 李亚春 on 13-12-16.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "EpointUpdateService.h"
#import "EpointStringUtil.h"
#import "RIButtonItem.h"
#import "UIAlertView+Blocks.h"
#import "SGActionView.h"

@implementation EpointUpdateService

+(BOOL)validateUpdateInfo:(NSObject *)obj vc:(EpointViewControllerBase *)vc flag:(int)flag
{
    NSString *bs = (NSString *)obj;
    NSString *version = [EpointStringUtil getXMLFormatedAttribute:bs :@"version"];
    if(version.length>0)
    {
        NSString *clientVersion = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"];
        NSString *remoteVersion = [EpointStringUtil getXMLFormatedAttribute:bs :@"version"];
        if ([clientVersion isEqualToString:remoteVersion])
        {
            if (flag==1) {
                [vc toastMessage:@"当前已经是最新版本！"];
            }
            return NO;
        }
        else
        {
            NSString *updateinfo = [[[EpointStringUtil getXMLFormatedAttribute:bs :@"updateinfo"]stringByReplacingOccurrencesOfString:@"\\n" withString:@"\n"]stringByReplacingOccurrencesOfString:@"\\t" withString:@"\t"];
            NSString *updatetitle = [EpointStringUtil getXMLFormatedAttribute:bs :@"updatetitle"];
            NSString *confirmtitle = [EpointStringUtil getXMLFormatedAttribute:bs :@"confirmtitle"];
            NSString *updateUrl = [EpointStringUtil getXMLFormatedAttribute:bs :@"updateurl"];
            
            [self alertUpdateOrigion:updatetitle confirmText:confirmtitle updateInfo:updateinfo updateUrl:updateUrl];
            
            
            NSString *must = [EpointStringUtil getXMLFormatedAttribute:bs :@"must"];
            if ([must isEqualToString:@"1"])
            {
                return YES;
            }
            else
            {
                return NO;
            }
        }
        
    }
    else
    {
        [vc toastMessage:@"网络连接超时，请检查网络配置！"];
        return NO;
    }
    
}

+(void)update:(NSString *)updateUrl
{
    NSString *deviceType = @"ipad";
    if ([NSLocalizedString(@"deviceType", @"") isEqualToString:@"1"]) {
        deviceType = @"iphone";
    }
    NSString *url = NSLocalizedString(@"Epoint_MobileManager_URL", @"");
    url = [url stringByReplacingOccurrencesOfString:@"services/MobileOaManage?wsdl" withString:@"mobileconfig/updateclient"];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:updateUrl]];
}

//系统对话框提示更新
+(void)alertUpdateOrigion:(NSString *)title confirmText:(NSString *)confirmText updateInfo:(NSString *)updateInfo updateUrl:(NSString *)updateurl
{
    RIButtonItem *okItem = [RIButtonItem item];
    okItem.label = confirmText;
    okItem.action = ^
    {
        [self update:updateurl];
    };
    
    RIButtonItem *cancelItem = [RIButtonItem item];
    cancelItem.label = @"取消";
    cancelItem.action = nil;
    
    UIAlertView *alter = [[UIAlertView alloc]initWithTitle:title message:updateInfo cancelButtonItem:cancelItem otherButtonItems:okItem,nil];
    
    [alter show];
}

//SGAlert对话框提示更新
+(void)alertUpdateSGAlert:(NSString *)title confirmText:(NSString *)confirmText updateInfo:(NSString *)updateInfo updateUrl:(NSString *)updateurl
{
    [SGActionView showAlertWithTitle:title
                             message:updateInfo
                     leftButtonTitle:@"取消"
                    rightButtonTitle:confirmText
                      selectedHandle:^(NSInteger index){
                          if (index==1) {
                              [self update:updateurl];
                          }
                      }];
}

@end
